unit uCreateUser;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, DB, OracleData;

type
  TfmCreateUser = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    edLogin: TEdit;
    edName: TEdit;
    edMiddleName: TEdit;
    edLastName: TEdit;
    dtpBirthDate: TDateTimePicker;
    cbDepartment: TComboBox;
    cbPosition: TComboBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    odsCreateUser: TOracleDataSet;
    procedure cbDepartmentChange(Sender: TObject);
  private
    { Private declarations }
    procedure ClearFields;
  public
    { Public declarations }
    Login : string;
    Name  : string;
    MiddleName : string;
    LastName : string;
    BirthDate : string;
    DepartmentId : string;
    PositionId : string;

    function Execute(windiwCaption : string) : boolean;
  end;

var
  fmCreateUser: TfmCreateUser;

implementation

{$R *.dfm}

uses uMain;

{ TfmCreateUser }

procedure TfmCreateUser.ClearFields;
begin
  edLogin.Text := '';
  edName.Text := '';
  edMiddleName.Text := '';
  edLastName.Text := '';
  cbPosition.Items.Clear;
  cbDepartment.Items.Clear;
end;

function TfmCreateUser.Execute(windiwCaption: string): boolean;
begin
  result := false;

  ClearFields;

  odsCreateUser.Close;
  odsCreateUser.sql.Text := 'select '+'dpmt_id'+' as id, name from '+'departments';
  odsCreateUser.Open;

  odsCreateUser.First;

  while not (odsCreateUser.eof) do
  begin
    cbDepartment.Items.AddObject(odsCreateUser.FieldByName('NAME').AsString, TObject(odsCreateUser.FieldByName('ID').AsInteger));

    odsCreateUser.Next;
  end;

  odsCreateUser.Close;
  odsCreateUser.sql.Text := 'select '+'pstn_id'+' as id, name from '+'positions';
  odsCreateUser.Open;

  odsCreateUser.First;

  while not (odsCreateUser.eof) do
  begin
    cbPosition.Items.AddObject(odsCreateUser.FieldByName('NAME').AsString, TObject(odsCreateUser.FieldByName('ID').AsInteger));

    odsCreateUser.Next;
  end;

  odsCreateUser.Close;

  BitBtn1.Enabled := false;

  if (ShowModal = mrOk) then
  begin
    Login := edLogin.Text;
    Name := edName.Text;
    MiddleName := edMiddleName.Text;
    LastName := edLastName.Text;
    BirthDate := FormatDateTime('dd.mm.yyyy', dtpBirthDate.Date);
    PositionId := IntToStr(integer(cbPosition.Items.Objects[cbPosition.ItemIndex]));
    DepartmentId := IntToStr(integer(cbDepartment.Items.Objects[cbDepartment.ItemIndex]));

    result := true;
  end;
end;

procedure TfmCreateUser.cbDepartmentChange(Sender: TObject);
begin
  BitBtn1.Enabled := (cbDepartment.ItemIndex >= 0) and (cbPosition.ItemIndex >= 0);
end;

end.
